package cn.sciento.boot.api.customize.commons.vo;

import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import org.apache.commons.lang3.StringUtils;

public class ApiCustomizePoint implements Serializable {
  private static final long serialVersionUID = 8904766040763741619L;
  
  public static final String FIELD_POINT_ID = "pointId";
  
  public static final String FIELD_SERVICE_NAME = "serviceName";
  
  public static final String FIELD_PACKAGE_NAME = "packageName";
  
  public static final String FIELD_CLASS_NAME = "className";
  
  public static final String FIELD_METHOD_NAME = "methodName";
  
  public static final String FIELD_METHOD_ARGS = "methodArgs";
  
  public static final String FIELD_METHOD_DESCRIPTION = "methodDescription";
  
  @ApiModelProperty("服务名")
  private String serviceName;
  
  @ApiModelProperty("包名")
  private String packageName;
  
  @ApiModelProperty("类名")
  private String className;
  
  @ApiModelProperty("方法名")
  private String methodName;
  
  @ApiModelProperty("方法参数列表")
  private String methodArgs;
  
  @ApiModelProperty("规则编码集合")
  private List<String> ruleCodes;
  
  private static final String HASH_KEY_PATTERN = "%s#%s#%s#%s";
  
  public ApiCustomizePoint() {}
  
  public ApiCustomizePoint(String serviceName, String packageName, String className, String methodName, String methodArgs) {
    this.serviceName = serviceName;
    this.packageName = packageName;
    this.className = className;
    this.methodName = methodName;
    this.methodArgs = methodArgs;
  }
  
  public static String generateKey(String serviceName) {
    return "hpfm:customize:point_rule:" + serviceName;
  }
  
  public static ApiCustomizePoint generate(String serviceName, String key, String value) {
    String[] arrK = key.split("#");
    ApiCustomizePoint point = new ApiCustomizePoint(serviceName, arrK[0], arrK[1], arrK[2], arrK[3]);
    point.setRuleCodes(Arrays.asList(value.split(",")));
    return point;
  }
  
  public String generateHashKey() {
    return String.format("%s#%s#%s#%s", new Object[] { this.packageName, this.className, this.methodName, StringUtils.defaultIfBlank(this.methodArgs, "none") });
  }
  
  public String getServiceName() {
    return this.serviceName;
  }
  
  public ApiCustomizePoint setServiceName(String serviceName) {
    this.serviceName = serviceName;
    return this;
  }
  
  public String getPackageName() {
    return this.packageName;
  }
  
  public ApiCustomizePoint setPackageName(String packageName) {
    this.packageName = packageName;
    return this;
  }
  
  public String getClassName() {
    return this.className;
  }
  
  public ApiCustomizePoint setClassName(String className) {
    this.className = className;
    return this;
  }
  
  public String getMethodName() {
    return this.methodName;
  }
  
  public ApiCustomizePoint setMethodName(String methodName) {
    this.methodName = methodName;
    return this;
  }
  
  public String getMethodArgs() {
    return this.methodArgs;
  }
  
  public ApiCustomizePoint setMethodArgs(String methodArgs) {
    this.methodArgs = methodArgs;
    return this;
  }
  
  public List<String> getRuleCodes() {
    return this.ruleCodes;
  }
  
  public ApiCustomizePoint setRuleCodes(List<String> ruleCodes) {
    this.ruleCodes = ruleCodes;
    return this;
  }
  
  public String toString() {
    return (new StringJoiner(", ", ApiCustomizePoint.class.getSimpleName() + "[", "]"))
      .add("serviceName='" + this.serviceName + "'")
      .add("packageName='" + this.packageName + "'")
      .add("className='" + this.className + "'")
      .add("methodName='" + this.methodName + "'")
      .add("methodArgs='" + this.methodArgs + "'")
      .add("ruleCodes=" + this.ruleCodes)
      .toString();
  }
}
